package com.kylin.module.shop.business;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import com.kylin.common.dict.orm.Dict;
import com.kylin.common.exception.BusinessException;
import com.kylin.hibernate.BaseHibernateDAO;
import com.kylin.module.shop.forms.ContentForm;
import com.kylin.module.shop.forms.TradeForm;
import com.kylin.module.shop.orm.Content;

public class ContentBlogic {
	protected static Logger log = Logger.getLogger(ContentBlogic.class);
	public void addContent(ContentForm form) throws BusinessException{
		Content obj = new Content();
	    obj.setCompany(form.getCompany());
	    if(form.getCatProductId() != null && !form.getCatProductId().equals("")){
	    	Dict catp = new Dict(form.getCatProductId());
	    	obj.setCatproduct(catp);
	    }
	    if(form.getCurrency() != null && !form.getCurrency().equals("")){
	    	Dict currency = (Dict) BaseHibernateDAO.findById(Dict.class, form.getCurrency());
	    	obj.setCurrency (currency);
	    }
	    if(form.getUnit() != null && !form.getUnit().equals("")){
	    	Dict unit = (Dict) BaseHibernateDAO.findById(Dict.class, form.getUnit());
	    	obj.setUnit(unit);
	    }
	    obj.setTitle       ( form.getTitle       () );
	    obj.setContent     ( form.getContent     () );
	    obj.setCountry     (form.getCompany().getUser().getCountry());
	    obj.setType        ( form.getType        () );
	    obj.setPpackage    ( form.getPpackage    () );
	    if(form.getDuedate() != null && !form.getDuedate().equals("")){
	    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		    try {
				obj.setDuedate     ( format.parse(form.getDuedate()) );
			} catch (ParseException e) {
				throw new BusinessException("有效期格式错误");
			}
	    }
	    obj.setPrice       ( form.getPrice       () );
	    obj.setQuantity    ( form.getQuantity    () );
	    obj.setAddress     ( form.getAddress     () );
	    obj.setEmail       ( form.getEmail       () );
	    obj.setPostcode    ( form.getPostcode    () );
	    obj.setPhoneCountry( form.getPhoneCountry() );
	    obj.setPhoneArea   ( form.getPhoneArea   () );
	    obj.setPhone       ( form.getPhone       () );
	    obj.setFaxCountry  ( form.getFaxCountry  () );
	    obj.setFaxArea     ( form.getFaxArea     () );
	    obj.setFax         ( form.getFax         () );
	    obj.setContact     ( form.getContact     () );
	    obj.setLanguage    ( form.getCompany().getLanguage    () );
	    obj.setCreatedby   ( form.getCreatedby   () );
	    obj.setCreatedon(new Date());
	    BaseHibernateDAO.save(obj);
	}

	public Content getContent(String sid) throws BusinessException{
		Long lid = new Long(sid);
		return (Content)BaseHibernateDAO.findById(Content.class, lid);
	}

	public Content updateContent(ContentForm form) throws BusinessException{
		Content obj = (Content) BaseHibernateDAO.findById(Content.class, form.getId());
		if(form.getCatProductId() != null ){
	    	if( !form.getCatProductId().equals("")){
		    	Dict catp = new Dict(form.getCatProductId());
		    	obj.setCatproduct(catp);
	    	}else
	    		obj.setCatproduct(null);
	    }
	    if(form.getCurrency() != null){
	    	 if(!form.getCurrency().equals("")){
	    	Dict currency = (Dict) BaseHibernateDAO.findById(Dict.class, form.getCurrency());
	    	obj.setCurrency (currency);
	    	 }else
	    		 obj.setCurrency(null);
	    }
	    if(form.getUnit() != null){
	    	 if(!form.getUnit().equals("") ){
	    	Dict unit = (Dict) BaseHibernateDAO.findById(Dict.class, form.getUnit());
	    	obj.setUnit(unit);
	    	 }else
	    		 obj.setUnit(null);
	    }
	    obj.setTitle       ( form.getTitle       () );
	    obj.setContent     ( form.getContent     () );
	    if(form.getType()!=null)
	    	obj.setType  ( form.getType        () );
	    obj.setPpackage    ( form.getPpackage    () );
	    if(form.getDuedate() != null ){
	    	if(!form.getDuedate().equals("") ){
		    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			    try {
					obj.setDuedate     ( format.parse(form.getDuedate()) );
				} catch (ParseException e) {
					throw new BusinessException("有效期格式错误");
				}
	    	}else
	    		obj.setDuedate(null);
	    }
	    obj.setPrice       ( form.getPrice       () );
	    obj.setQuantity    ( form.getQuantity    () );
	    obj.setAddress     ( form.getAddress     () );
	    obj.setEmail       ( form.getEmail       () );
	    obj.setPostcode    ( form.getPostcode    () );
	    obj.setPhoneCountry( form.getPhoneCountry() );
	    obj.setPhoneArea   ( form.getPhoneArea   () );
	    obj.setPhone       ( form.getPhone       () );
	    obj.setFaxCountry  ( form.getFaxCountry  () );
	    obj.setFaxArea     ( form.getFaxArea     () );
	    obj.setFax         ( form.getFax         () );
	    obj.setContact     ( form.getContact     () );
	    obj.setLastupdatedon(new Date());
	    BaseHibernateDAO.update(obj);
	    return obj;
	}
	
	/**
	 * 删除指定 id 的贸易信息
	 * @param id
	 * @throws BusinessException
	 */
	public void deleteContent(Long id) throws BusinessException {
		BaseHibernateDAO.deleteById(Content.class, id);
	}
	
	/**
	 * 从后台添加供求信息
	 * @param form
	 * @throws BusinessException
	 */
	public void addTrade(TradeForm form) throws BusinessException
	{
		log.debug("开始添加后台贸易信息，标题为："+form.getTitle());
		Content obj = new Content();
	    if(form.getCatProductId() != null && !form.getCatProductId().equals("")){
	    	Dict catp = (Dict) BaseHibernateDAO.findById(Dict.class, form.getCatProductId());
	    	obj.setCatproduct(catp);
	    }
	    if(form.getCurrencyId() != null && !form.getCurrencyId().equals("")){
	    	Dict currency = (Dict) BaseHibernateDAO.findById(Dict.class, form.getCurrencyId());
	    	obj.setCurrency (currency);
	    }
	    if(form.getUnitId() != null && !form.getUnitId().equals("")){
	    	Dict unit = (Dict) BaseHibernateDAO.findById(Dict.class, form.getUnitId());
	    	obj.setUnit(unit);
	    }
	    
		obj.setAddress(form.getAddress());
		obj.setCompany(null);
		obj.setContact(form.getContact());
		obj.setContent(form.getContent());
		obj.setCountry(form.getCountry());
		obj.setCreatedon(new Date());
	    if(form.getDuedate() != null && !form.getDuedate().equals("")){
	    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		    try {
				obj.setDuedate(format.parse(form.getDuedate()));
			} catch (ParseException e) {
				throw new BusinessException("有效期格式错误");
			}
	    }
		obj.setEmail(form.getEmail());
		obj.setFax(form.getFax());
		obj.setFaxArea(form.getFaxArea());
		obj.setFaxCountry(form.getFaxCountry());
		obj.setLanguage(form.getLanguage());
		obj.setPhone(form.getPhone());
		obj.setPhoneArea(form.getPhoneArea());
		obj.setPhoneCountry(form.getPhoneCountry());
		obj.setPostcode(form.getPostcode());
		obj.setPpackage(form.getPpackage());
		obj.setPrice(form.getPrice());
		obj.setQuantity(form.getQuantity());
		obj.setTitle(form.getTitle());
		obj.setType(form.getType());
		BaseHibernateDAO.save(obj);
	}
}
